package org.example;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;

public class ClazzTransformer implements ClassFileTransformer {
    @Override
    public byte[] transform(
            ClassLoader loader,
            String className,
            Class<?> classBeingRedefined,
            ProtectionDomain protectionDomain,
            byte[] classfileBuffer) {
        System.out.println("ClazzTransformer类的transform方法执行了,className=" + className);
        try {
            ClassPool cp = ClassPool.getDefault();
            CtClass cc = cp.get("meituan.bytecode.javassist.Base");
            CtMethod m = cc.getDeclaredMethod("process");
            m.insertBefore(" System.out.println(\"方法开始了111----start\"); ");
            m.insertAfter(" System.out.println(\"方法结束了222----end\"); ");
            m.insertAfter(" System.out.println(); ");
            return cc.toBytecode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
